﻿using ManagementCMS.Tool;
using ManagementCMS.Tool.Log4;
using ManagementCMS.Tool.ServiceStackRedis;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weixin.Core.config;
using Weixin.Core.Model;

namespace Weixin.Core
{
    public class WxAuthonHelp
    {
        /// <summary>
        /// 微信授权获取  用户基本信息
        /// </summary>
        /// <param name="callbackUrl"></param>
        /// <returns></returns>
        public static string GetAuthorizeUrl(string callbackUrl)
        {
            string state = "JeffreySu - " + DateTime.Now.Millisecond;
            string _requestUrl = string.Format("{0}{1}", WxConfig.WxOauthUrl, "BaseCallback?returnUrl=");
            //Log4Helper.WriteInfo("回调地址"+ _requestUrl);
            return OAuthApi.GetAuthorizeUrl(WxConfig.AppId,
                _requestUrl + callbackUrl.UrlEncode(),
                state, OAuthScope.snsapi_userinfo);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public static WxUserInfo GetWxUserInfo(string code)
        {

            WxUserInfo wxUserInfo = new WxUserInfo();
            try
            {
                var tokenResult = OAuthApi.GetAccessToken(WxConfig.AppId, WxConfig.AppSecret, code);
                var userCode=RedisHelper.Get<string>(tokenResult.openid);
                //Log4Helper.WriteInfo("redis缓存的用户"+userCode);

                //如果openid不存在
                if (string.IsNullOrEmpty(userCode))
                {
                    var userInfo = OAuthApi.GetUserInfo(tokenResult.access_token, tokenResult.openid);
                    if (userInfo != null)
                    {
                        wxUserInfo.openid = userInfo.openid;
                        wxUserInfo.usercode = EncodeSecurity.Md5by16Small(Guid.NewGuid().ToString(),"");
                        wxUserInfo.nickname = userInfo.nickname;
                        wxUserInfo.headimgurl = userInfo.headimgurl;
                        wxUserInfo.privilege = userInfo.privilege;
                        wxUserInfo.province = userInfo.province;
                        wxUserInfo.sex = userInfo.sex;
                        wxUserInfo.unionid = userInfo.unionid;
                        wxUserInfo.city = userInfo.city;
                        wxUserInfo.country = userInfo.country;
                        wxUserInfo.states = 1;
                    }
                }
                else
                {
                    wxUserInfo.openid = tokenResult.openid;
                    wxUserInfo.usercode = userCode;
                    wxUserInfo.states = 2;
                    //Log4Helper.WriteInfo("已经缓存的数据:"+userCode);
                }
            }
            catch (Exception ex)
            {
                Log4Helper.WriteError("微信获取用户基本信息",ex);
            }
            return wxUserInfo;
        }


    }
}
